<?php

namespace app\model;

use think\facade\Log;
use think\Model;

class CustomerInfo extends Model
{

    protected $table = 'customer_info';

    public function modifyDataById(int $customerId, array $data)
    {
        return $this->where(['id' => $customerId])->update($data);
    }

    public function customerList(array $params)
    {
        $fields = [
            '`id` as customer_id',
            '`username`',
            '`organization_name`',
            '`organization_quality`',
            '`user_overdue`',
            '`remark`'
        ];
        $execute = $this;
        if (!empty($params['keywords'])) {
            $execute = $execute->where('username', 'like', '%' . $params['keywords'] . '%');
        }
        return $execute
            ->field($fields)
            ->order('id', 'desc')
            ->paginate(10, false)
            ->toArray();
    }

    public function customerInfo(int $customerId)
    {
        $where = [
            'id' => $customerId,
        ];
        $field = [
            '`id` as customer_id',
            '`username`',
            '`organization_name`',
            '`organization_quality`',
            '`birthday`',
            '`marriage`',
            '`user_area`',
            '`user_overdue`',
            '`remark`'
        ];
        return $this
            ->where($where)
            ->field($field)
            ->findOrEmpty()
            ->toArray();
    }

    public function birthdayNotice()
    {
        $where = [
            'birthday_day' => date('md'),
        ];
        $field = [
            '`id` as customer_id',
            '`username`',
            '`birthday`',
        ];
        return $this
            ->where($where)
            ->field($field)
            ->select()
            ->toArray();
    }

    public function idKeyList()
    {
        $field = [
            '`id` as customer_id',
            '`username`',
            '`birthday`',
        ];
        return $this->column($field, 'id');
    }
}